<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\View;
use App\Models\User;

//小程序用户控制器
class UserController extends Controller
{

    use AuthenticatesUsers;

    /**
     * 用户列表主页
     * @return \Illuminate\Contracts\View\View
     */
    public function index()
    {
        return View::make('admin.user.index');
    }

    //ajax返回用户列表数据
    public function data(Request $request)
    {
        $model = User::query();
        if ($mobile = $request->get('mobile')) {
            $model->where('mobile',$mobile);
        }
        if ($username = $request->get('username')) {
            $model->where('username',$username);
        }
        if ($register_start = $request->get('register_start')) {
            $model->where('created_at','>=',$register_start);
        }
        if ($register_end = $request->get('register_end')) {
            $model->where('created_at','<=',$register_end);
        }

        $res = $model->orderBy('id','desc')->paginate($request->get('limit', 30));

        $user_model = new User();

        //todo  计算每个用户的一、二级好友数量，待优化。（左右值、orm自关联子查询）
        foreach ($res->items() as $item) {
            $total = $user_model->friends_num($item->id);
            $item['friends_num'] = $total;
        }

        $data = [
            'code' => 0,
            'msg' => '正在请求中...',
            'count' => $res->total(),
            'data' => $res->items()
        ];
        return Response::json($data);
    }

    /**
     * 用户详情
     * @param $id
     * @return \Illuminate\Contracts\View\View
     */
    public function detail($id)
    {
        $user = User::with('parent')->findOrFail($id);
        //上上级手机号
        if ($user->parent) {
            $p_p_mobile = User::where('id',$user->parent->pid)->value('mobile');
        }else {
            $p_p_mobile = '无';
        }

        return View::make('admin.user.detail',compact('user','p_p_mobile'));
    }

    //返回某用户的一级好友数据
    public function one_friends(Request $request) {
        $request->validate([
            'id'    =>  'exists:users'
        ]);
        $model = new User();
        $res = $model->get_users(['pid'=>$request->get('id')],$request->get('limit',30));
        $data = [
            'code' => 0,
            'msg' => '正在请求中...',
            'count' => $res->total(),
            'data' => $res->items()
        ];
        return Response::json($data);
    }

    //返回某用户的二级好友数据
    public function two_friends(Request $request) {
        $request->validate([
            'id'    =>  'exists:users'
        ]);
        //一级好友
        $one = User::where('pid',$request->get('id'))->orderBy('id','desc')->pluck('id');

        //二级好友
        $two = User::whereIn('pid',$one)->orderBy('id','desc')->paginate($request->get('limit',30));

        $data = [
            'code' => 0,
            'msg' => '正在请求中...',
            'count' => $two->total(),
            'data' => $two->items()
        ];
        return Response::json($data);
    }

}
